Ext.require([
    'Ext.form.field.ComboBox',
    'Ext.form.FieldSet',
    'Ext.tip.QuickTipManager',
    'Ext.data.*'
]);

Ext.require([
             'Ext.tree.*',
             'Ext.window.MessageBox',
             'Ext.data.*'
            ]);
Ext.require(['Ext.tab.*']);
Ext.require(['*']);


//~~~~~~~~~~~~~~~~~~~~~grid~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var wikiGrid;
var dummyData;
var wikiColumns;
var cellEditing;
var getLocalStore;

Ext.Loader.setConfig({
  enabled: true
});
Ext.Loader.setPath('Ext.ux', '../extjs/examples/ux');

Ext.require([
           'Ext.grid.*',
           'Ext.data.*',
           'Ext.ux.RowExpander',
           'Ext.selection.CheckboxModel'
       ]);

Ext.require([
             'Ext.selection.CellModel',
             'Ext.util.*',
             'Ext.state.*',
             'Ext.form.*',
             'Ext.ux.CheckColumn'
         ]);

Ext.onReady(function(){
	
	
	Ext.define('Company', {
      extend: 'Ext.data.Model',
      fields: [
          {name: 'company'},
          {name: 'price', type: 'float'},
          {name: 'change', type: 'float'},
          {name: 'pctChange', type: 'float'},
          {name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'},
          {name: 'industry'} 
       ]
  });
	
  // Array data for the grids
  dummyData = [
      ['3m Co',71.72,0.02,0.03,'9/1 12:00am', 'Manufacturing'],
      ['Alcoa Inc',29.01,0.42,1.47,'9/1 12:00am', 'Manufacturing']
  ];


  getLocalStore = Ext.create('Ext.data.ArrayStore', {
      model: 'Company',
      data: dummyData
  });
  
  wikiColumns =  [
                 Ext.create('Ext.grid.RowNumberer'),
                 {text: "Company", width: 200, dataIndex: 'company',editor: {}},
                 {text: "Price", renderer: Ext.util.Format.usMoney, dataIndex: 'price',editor: {}},
                 {text: "Change", dataIndex: 'change',editor: {}},
                 {text: "% Change", dataIndex: 'pctChange',editor: {}},
                 {text: "Last Updated", width: 135, editor: {}, renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'}
                 ];
  
  Ext.QuickTips.init();
  
  cellEditing = Ext.create('Ext.grid.plugin.CellEditing', {
      dbclicksToEdit: 1
  });
  
  
  wikiGrid = Ext.create('Ext.grid.Panel', {
	    store: getLocalStore,
//	    cm: wikiColumns,
	    columns: wikiColumns,
	    columnLines: true,
	    width: 400,
	    height: 200,
	    frame: true,
	    header:false,
	    multiSelect: true,
	    title: '',
	    iconCls: 'icon-grid',
	    renderTo: Ext.getBody(),
	    plugins: [cellEditing]
	});

});


//~~~~~~~~~~~~~~~~~~~~~~~~数据库下拉菜单~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

var databaseGroupCombo ;
var tableInfo;
var tableGroupCombo ;
var tableStore;

Ext.onReady(function() {
	
  Ext.define("DatabaseGroup", {
      extend: "Ext.data.Model",
      fields: [
          { name: "id" },
          { name: "name" }
      ]
  });

  var store = Ext.create("Ext.data.Store", {
      model: "DatabaseGroup",
      proxy: {
          type: "ajax",
          url: "wiki/getDatabaseInfo.json",
          reader: new Ext.data.JsonReader({ model: "DatabaseGroup" })
      }
  });

  databaseGroupCombo = Ext.create('Ext.form.field.ComboBox', {
	    fieldLabel: '数据库',
	    renderTo: Ext.getBody(),
	    displayField: 'name',
	    width: 250,
	    labelWidth: 40,
	    store: store,
	    typeAhead: false,
	    editable:false,
	    padding:'0 0 0 5',
        name: 'DatabaseGroup',
        triggerAction: 'all',
        displayField: 'name',
        valueField: 'id',
        queryMode: 'remote',
        emptyText:'请选择' 
	});
  
  databaseGroupCombo.on('change', function(field, newValue, oldValue, obj) {
	  
		  	   if (newValue == "" || newValue == undefined) {
		  		    return;
		  	   }
		  	   
		  	   tableInfo = [];
	   	       tableGroupCombo.store.loadData(tableInfo);
	   	       tableGroupCombo.setVisible(false);
	   	        
		       Ext.Ajax.request({
			   	    url: 'wiki/getTableInfo.json',
			   	    params: {
			   	    	dbId : newValue
			   	    },
			   	    success: function(response){
			   	        var text = response.responseText;
			   	        var datas = Ext.decode(response.responseText);
			   	        tableInfo = datas;
			   	        tableGroupCombo.store.loadData(tableInfo);
			   	        tableGroupCombo.setVisible(true);
			   	    }
		       });
		       
		});   
  
});


//~~~~~~~~~~~~~~~~~~~~~~~~数据表下拉菜单~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Ext.onReady(function() {
	
tableInfo = [];
	
tableStore = Ext.create("Ext.data.ArrayStore", {
    autoDestroy: true,
    idIndex: 0,
    fields:[
            'name',
            {name:'id',type:'string'}
           ],
    data: tableInfo
});



tableGroupCombo = Ext.create('Ext.form.field.ComboBox', {
	    fieldLabel: '数据表',
	    renderTo: Ext.getBody(),
	    displayField: 'name',
	    width: 300,
	    labelWidth: 40,
	    store: tableStore,
	    typeAhead: false,
	    editable:true,
	    padding:'0 0 0 5',
        name: 'TableGroup',
        triggerAction: 'all',
        displayField: 'name',
        valueField: 'id',
        queryMode: 'local',
        emptyText:'输入可补全。选中则立即查询该表',
        hidden:true
	});
});

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~viewport~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ext.onReady(function() {
             
             vp =  Ext.create('Ext.Viewport', {
                 layout: {
                     type: 'border',
                     padding: 5
                 },
                 defaults: {
                     split: true
                 },
                 items: [{
                     region: 'north',
                     collapsible: false,
                     title: '',
                     split: true,
                     height: 35,
                     minHeight: 35,
                     bodyStyle:'background-color:#DDE7F4',
                     border:false,
                     html: '<span style=\"font-weight:bold;font-size:25px; color:#03336F;\">Easy Wiki</span> | <span style=\"font-size:13px; color:#03336F;\">良无限测试Wiki文件生成工具</span>'
                 },{
                     region: 'center',
                     layout: 'border',
                     border: true,
                     resizable:false,
                     title: '操作台',
                   	 dockedItems: [{
                         xtype: 'toolbar',
                         dock: 'top',
                         minHeight: 30,
                         items: [
							{
							    text: '运行SQL',
							    iconCls:'execute2-button',
							    handler: function(){
							    	
							    	Ext.define('Company', {
							    	      extend: 'Ext.data.Model',
							    	      fields: [
							    	          {name: 'company'}
							    	       ]
							    	  });
							    		
							    	  // Array data for the grids
							    	  dummyData = [
							    	      ['3m Co',71.72,0.02,0.03,'9/1 12:00am', 'Manufacturing'],
							    	      ['Alcoa Inc',29.01,0.42,1.47,'9/1 12:00am', 'Manufacturing']
							    	  ];


							    	  getLocalStore = Ext.create('Ext.data.ArrayStore', {
							    	      model: 'Company',
							    	      data: dummyData
							    	  });
							    	  
							    	  wikiColumns = [
							    	                 Ext.create('Ext.grid.RowNumberer'),
							    	                 {text: "Company", width: 200, dataIndex: 'company',editor: {}}
							    	                 ];
							    	  
							    	  wikiGrid.reconfigure(getLocalStore, new Ext.grid.ColumnModel('Company'));
							    }
							},"-",databaseGroupCombo,"-",tableGroupCombo 
                         ]
                     }],
                     items: [
								{
								    region: 'north',
								    layout: 'fit',
								    border: true,
								    resizable:true,
								    collapsible: true,
				                    split: true,
				                    height: 100,
				                    minHeight: 100,
				                    header:false,
								    title: '',
								    items: [{
									            xtype: 'textareafield',
									            name: 'textarea1',
									            fieldLabel: '',
							                    border: false,
							                    anchor    : '100%',
							                    emptyText : '请输入SQL，仅支持Select语句'
									        }]
								},{
								    region: 'center',
								    layout: 'fit',
								    border: true,
								    resizable:true,
								    collapsible: false,
				                    split: true,
				                    header:true,
				                    minHeight: 100,
				                    id:'r1',
								    title: 'Wiki生成区',
								    items: [
								            wikiGrid
								           ]
								}
						    ]
                 }]
             });
         });
 